/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package drivemachine;

import drivemachine.data2.CompareEntry;
import drivemachine.data2.FolderEntry;
import java.util.Hashtable;
import java.util.Vector;

/**
 *
 * @author positron
 */
//TODO: invent some hack not to inherit unnececary features
public class Comparator {
	public static final int CAP = 10000;
	CompareEntry root;
	//Scanner s1,s2;
	FolderEntry root1, root2;

	public CompareEntry getRoot() {
		return root;
	}

/*	public Scanner getS1() {
		return s1;
	}

	public Scanner getS2() {
		return s2;
	}*/

	public FolderEntry getSide(int i) {
		if(i==0) return root1; else return root2;
	}

	public void compare(FolderEntry f1, FolderEntry f2) {//Scanner sc1, Scanner sc2) {
		/*if (!sc1.getRoot().getFileName().toLowerCase().equals( sc2.getRoot().getFileName().toLowerCase() )) {
			System.out.println("Cannot compare different paths!");
			return;
		}*/
		if (!f1.getPath().equals( f2.getPath()) ) {
			System.out.println("Cannot compare different paths!");
			return;
		}
		/*s1 = sc1;
		s2 = sc2;*/
		root = new CompareEntry(f1, f2);
		root.traverse();
	}

	private  CompareEntry compare(FolderEntry f1, FolderEntry f2, Scanner sc1, Scanner sc2) {
		//if (!f1.path.equals(f2.path)) throw new RuntimeException("Cannot compare different paths!");
		/*assert(f1.path.equals(f2.path));
		CompareEntry entry=null;
		if (!f1.date.equals(f2.date)) {
			entry = new CompareEntry(f1,f2);
			entry.diffDate = true;
			System.out.println(f1 + " vs " + f2 + " differ in dates");
		}
		if (f1.selfsize != f2.selfsize) {
			if (entry==null)entry=new CompareEntry(f1,f2);
			entry.diffSelfsize = true;
			System.out.println(f1 + " vs " + f2 + " differ in self sizes");
		}
		if (f1.subsize != f2.subsize) {
			if (entry==null)entry=new CompareEntry(f1,f2);
			entry.diffSubsize = true;
			System.out.println(f1 + " vs " + f2 + " differ in contents");
			Vector<String> c1 = f1.children, c2 = f2.children;
			FolderEntry e1,e2;
			for (String s1 : c1) {
				e2 = sc2.table.get(s1);
				e1 = sc1.table.get(s1);
				if (e2 == null) {
					System.out.println(f1 + " vs " + f2 + ": child added: " + s1);
					CompareEntry child = new CompareEntry(e1, true, "[new]");
					if (child!=null) {
						entry.children.add(child);
						folders.add(child);
						table.put(child.path, child);
					}
				} else {
					CompareEntry child = compare(e1, e2, sc1, sc2);
					if (child!=null) {
						entry.children.add(child);
						folders.add(child);
						table.put(child.path, child);
					}
				}
			}
			for (String s2 : c2) {
				e2 = sc2.table.get(s2);
				e1 = sc1.table.get(s2);
				if (e1 == null) {
					System.out.println(f1 + " vs " + f2 + ": child removed: " + s2);
					CompareEntry child = new CompareEntry(e2, false, "[del]");
					if (child!=null) {
						entry.children.add(child);
						folders.add(child);
						table.put(child.path, child);
					}
				}
			}
		}
		return entry;*/
		return null;
	}
}
